package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_news;

import cn.com.api.service.ApiT_newsService;

import cn.com.util.*;
import cn.com.util.*;


public class T_newsBaseController {

	private static Logger logger = Logger.getLogger(T_newsBaseController.class);
	
	@Autowired
	private ApiT_newsService apiT_newsService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "t_news-list")
	public ModelAndView t_news_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_news_list.jsp");
		
		List<ApiT_news> t_newss = apiT_newsService.findT_newss();
		mv.addObject("t_newss", t_newss);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "t_news-add-pre")
	public ModelAndView t_news_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_news_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiT_news t_news = apiT_newsService.findT_newsById(Integer.valueOf(id));
			mv.addObject("t_news",t_news);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "t_news-add")
	public void t_news_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String rid = request.getParameter("rid");
		String titles = request.getParameter("titles");
		String ctype = request.getParameter("ctype");
		String keyword = request.getParameter("keyword");
		String contents = request.getParameter("contents");
		String appdate = request.getParameter("appdate");
		String origin = request.getParameter("origin");
		String appip = request.getParameter("appip");
		String appuser = request.getParameter("appuser");
		String modtype = request.getParameter("modtype");
		String enable = request.getParameter("enable");
		String accept = request.getParameter("accept");
		String ctype_b = request.getParameter("ctype_b");
		String city_id = request.getParameter("city_id");
		String picname = request.getParameter("picname");
		String c_order = request.getParameter("c_order");
		String product_id = request.getParameter("product_id");
		String price = request.getParameter("price");
		String is_delete = request.getParameter("is_delete");
		String status = request.getParameter("status");
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiT_news t_news = new ApiT_news();
            /*
		    t_news.setRid(rid);
		    t_news.setTitles(titles);
		    t_news.setCtype(ctype);
		    t_news.setKeyword(keyword);
		    t_news.setContents(contents);
		    t_news.setAppdate(appdate);
		    t_news.setOrigin(origin);
		    t_news.setAppip(appip);
		    t_news.setAppuser(appuser);
		    t_news.setModtype(modtype);
		    t_news.setEnable(enable);
		    t_news.setAccept(accept);
		    t_news.setCtype_b(ctype_b);
		    t_news.setCity_id(city_id);
		    t_news.setPicname(picname);
		    t_news.setC_order(c_order);
		    t_news.setProduct_id(product_id);
		    t_news.setPrice(price);
		    t_news.setIs_delete(is_delete);
		    t_news.setStatus(status);
	        */
			apiT_newsService.insertT_news(t_news);
		}
		//edit
		else{
			ApiT_news t_news = apiT_newsService.findT_newsById(Integer.valueOf(id));
	        /*
		    t_news.setRid(rid);
		    t_news.setTitles(titles);
		    t_news.setCtype(ctype);
		    t_news.setKeyword(keyword);
		    t_news.setContents(contents);
		    t_news.setAppdate(appdate);
		    t_news.setOrigin(origin);
		    t_news.setAppip(appip);
		    t_news.setAppuser(appuser);
		    t_news.setModtype(modtype);
		    t_news.setEnable(enable);
		    t_news.setAccept(accept);
		    t_news.setCtype_b(ctype_b);
		    t_news.setCity_id(city_id);
		    t_news.setPicname(picname);
		    t_news.setC_order(c_order);
		    t_news.setProduct_id(product_id);
		    t_news.setPrice(price);
		    t_news.setIs_delete(is_delete);
		    t_news.setStatus(status);
			*/
			apiT_newsService.updateT_news(t_news);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "t_news-delete")
	public void t_news_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiT_newsService.deleteT_news(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}



